package com.ma5d.driver.driver.service.impl;

import com.ma5d.driver.common.constant.RedisConstant;
import com.ma5d.driver.common.result.Result;
import com.ma5d.driver.common.util.AuthContextHolder;
import com.ma5d.driver.driver.client.DriverInfoFeignClient;
import com.ma5d.driver.driver.service.DriverService;
import com.ma5d.driver.model.form.DriverFaceModelForm;
import com.ma5d.driver.model.form.UpdateDriverAuthInfoForm;
import com.ma5d.driver.model.vo.driver.DriverLoginVo;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class DriverServiceImpl implements DriverService {

    @Resource
    private DriverInfoFeignClient driverInfoFeignClient;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public String login(String code) {
        String driverId = driverInfoFeignClient.login(code).getData();
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token, driverId, RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
        return token;
    }

    public DriverLoginVo getDriverLoginInfo() {
        Long driverId = AuthContextHolder.getUserId();
        Result<DriverLoginVo> loginVoResult = driverInfoFeignClient.getDriverLoginInfo(driverId);
        return loginVoResult.getData();
    }

    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        Long userId = AuthContextHolder.getUserId();
        updateDriverAuthInfoForm.setDriverId(userId);
        driverInfoFeignClient.updateDriverAuthInfo(updateDriverAuthInfoForm);
        return true;
    }

    @Override
    public Boolean createDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        Result<Boolean> driverFaceModel = driverInfoFeignClient.createDriverFaceModel(driverFaceModelForm);
        return driverFaceModel.getData();
    }
}
